package com.ruoyi.service.basic.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.basic.MesWorkstation;
import com.ruoyi.mapper.basic.MesWorkstationMapper;
import com.ruoyi.service.basic.IMesWorkstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工位信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class MesWorkstationServiceImpl implements IMesWorkstationService {
    @Autowired
    private MesWorkstationMapper mesWorkstationMapper;

    /**
     * 查询工位信息
     * 
     * @param workstationId 工位信息主键
     * @return 工位信息
     */
    @Override
    public MesWorkstation selectMesWorkstationByWorkstationId(Long workstationId) {
        return mesWorkstationMapper.selectMesWorkstationByWorkstationId(workstationId);
    }

    /**
     * 查询工位信息列表
     * 
     * @param mesWorkstation 工位信息
     * @return 工位信息
     */
    @Override
    public List<MesWorkstation> selectMesWorkstationList(MesWorkstation mesWorkstation) {
        return mesWorkstationMapper.selectMesWorkstationList(mesWorkstation);
    }

    /**
     * 新增工位信息
     * 
     * @param mesWorkstation 工位信息
     * @return 结果
     */
    @Override
    public int insertMesWorkstation(MesWorkstation mesWorkstation) {
        mesWorkstation.setCreateTime(DateUtils.getNowDate());
        return mesWorkstationMapper.insertMesWorkstation(mesWorkstation);
    }

    /**
     * 修改工位信息
     * 
     * @param mesWorkstation 工位信息
     * @return 结果
     */
    @Override
    public int updateMesWorkstation(MesWorkstation mesWorkstation) {
        mesWorkstation.setUpdateTime(DateUtils.getNowDate());
        return mesWorkstationMapper.updateMesWorkstation(mesWorkstation);
    }

    /**
     * 批量删除工位信息
     * 
     * @param workstationIds 需要删除的工位信息主键
     * @return 结果
     */
    @Override
    public int deleteMesWorkstationByWorkstationIds(Long[] workstationIds) {
        return mesWorkstationMapper.deleteMesWorkstationByWorkstationIds(workstationIds);
    }

    /**
     * 删除工位信息
     * 
     * @param workstationId 工位信息主键
     * @return 结果
     */
    @Override
    public int deleteMesWorkstationByWorkstationId(Long workstationId) {
        return mesWorkstationMapper.deleteMesWorkstationByWorkstationId(workstationId);
    }

    /**
     * 校验工位编码是否唯一
     * 
     * @param mesWorkstation 工位信息
     * @return 结果
     */
    @Override
    public String checkWorkstationCodeUnique(MesWorkstation mesWorkstation) {
        Long workstationId = StringUtils.isNull(mesWorkstation.getWorkstationId()) ? -1L
                : mesWorkstation.getWorkstationId();
        MesWorkstation info = mesWorkstationMapper.checkWorkstationCodeUnique(mesWorkstation.getWorkstationCode());
        if (StringUtils.isNotNull(info) && info.getWorkstationId().longValue() != workstationId.longValue()) {
            return "1"; // 不唯一
        }
        return "0"; // 唯一
    }

    /**
     * 查询启用状态的工位列表
     * 
     * @return 工位列表
     */
    @Override
    public List<MesWorkstation> selectActiveWorkstationList() {
        return mesWorkstationMapper.selectActiveWorkstationList();
    }

    /**
     * 根据车间ID查询工位列表
     * 
     * @param workshopId 车间ID
     * @return 工位列表
     */
    @Override
    public List<MesWorkstation> selectMesWorkstationByWorkshopId(Long workshopId) {
        return mesWorkstationMapper.selectMesWorkstationByWorkshopId(workshopId);
    }
}